package com.bruce.springbootDemo.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @param
 * @author bruce
 * @date 18/11/22 15:11
 * @Description: 描述--用户相关的信息拦截
 * @return
 */
@Configuration
public class UserInterceptors implements HandlerInterceptor {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 进controller前调用此方法
     *
     * @param request  请求
     * @param response 相应
     * @param handler  参数
     * @return true 就通过拦截器，false就拦截不让通过
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("用户拦截器：" + request.getSession().getId());
        return true;
    }

    /**
     * controller处理完了,渲染页面前，就会调用此方法
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        logger.debug("用户拦截器：" + request.getSession().getId());
    }

    /**
     * 页面渲染完了调用此方法,类似finally,用来清理某些资源
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}
